home *** CD-ROM | disk | FTP | other *** search
/ MacWorld Secrets (4th Edition) / Mac Secrets CD 4th Ed.toast / Apple Advanced Technologies / Apple Speech Technologies 1.5 / PlainTalk Developer Info / Speech Recognition Manager SDK / SR Sample Code / IM SR Example / main.c next >
Text File  |  1996-09-18  |  7KB  |  290 lines

  1. // Speech Recognition Example
  2. //             combines routines from IM document into application
  3. //             uses MetroWerks' SIOUX window 
  4.  
  5. #include <SpeechRecognition.h>
  6.  
  7. #include <AppleEvents.h>
  8. #include <Devices.h>
  9. #include <Dialogs.h>
  10. #include <Events.h>
  11. #include <Fonts.h>
  12. #include <Quickdraw.h>
  13. #include <Menus.h>
  14. #include <Resources.h>
  15. #include <TextEdit.h>
  16. #include <ToolUtils.h>
  17. #include <Windows.h>
  18.  
  19. #include <SIOUX.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. // define this to be true if you want to use SR callbacks
  24. #define SR_CALLBACKS    0
  25.  
  26. #define    rMenuBar                128        /* Menubar resource id */
  27.  
  28. #define    mApple                    128        /* Apple menu resource id */
  29.  
  30. #define    mFile                    129        /* File menu resource id */
  31.  
  32. // External Prototypes
  33. extern SRRecognitionSystem    MyInitSpeechRecognition (void);
  34. extern SRLanguageModel         MyBuildLanguageModel (SRRecognitionSystem mySystem);
  35. extern OSErr                MyInstallSpeechAEHandlers (void);
  36. extern pascal OSErr            MyInstallNotificationCallBack (SRRecognizer recognizer);
  37. extern pascal void            MyRemoveNotificationCallBack (SRRecognizer recognizer);
  38. extern pascal void            MyIdleCheckForSpeechResult (void);
  39.  
  40. // Internal Prototypes
  41. Boolean                     Initialize (void);
  42. void                         EventLoop (void);
  43. void                         Terminate (void);
  44.  
  45. void                        HandleMouseDown (EventRecord * event);
  46. void                         HandleKeyDown (unsigned char keyChar);
  47. void                         HandleMenuSelect (long menuChoice);
  48.  
  49. pascal OSErr                 DummyAEHandler (AppleEvent *theAEevt, AppleEvent* reply, long refcon);
  50. pascal OSErr                 HandleQuitAE (AppleEvent *theAEevt, AppleEvent* reply, long refcon);
  51.  
  52. // Globals
  53. Boolean                gFinished = false;
  54.  
  55. SRRecognitionSystem    gRecSystem = NULL;
  56. SRLanguageModel        gLanguageModel = NULL;
  57. SRRecognizer        gRecognizer = NULL;
  58. Boolean                gListening = false;
  59.  
  60. // Routines
  61. void main (void)
  62. {
  63.     if ((gListening = Initialize ()) == true)
  64.         EventLoop ();
  65.     else
  66.         {
  67.         SysBeep (0);
  68.         printf ("Initialization failed.\n");
  69.         }
  70.         
  71.     Terminate ();
  72. }
  73.  
  74. Boolean Initialize (void)
  75. {
  76.     Boolean        successfulStartup = false;
  77.     OSErr        myErr = noErr;    
  78.     short        index;
  79.     EventRecord    event;
  80.     
  81.     InitGraf((Ptr) &qd.thePort);
  82.     InitFonts();
  83.     InitWindows();
  84.     InitMenus();
  85.     TEInit();
  86.     InitDialogs(nil);
  87.     InitCursor();
  88.  
  89.     for (index = 1; index <= 3; index++)
  90.         EventAvail(everyEvent, &event);
  91.  
  92.     if (!myErr)
  93.         myErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, 
  94.                                 NewAEEventHandlerProc (DummyAEHandler), 0, false);
  95.     if (!myErr)
  96.         myErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, 
  97.                                 NewAEEventHandlerProc (DummyAEHandler), 0, false);
  98.     if (!myErr)
  99.         myErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, 
  100.                                 NewAEEventHandlerProc (DummyAEHandler), 0, false);
  101.     if (!myErr)
  102.         myErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, 
  103.                                 NewAEEventHandlerProc (HandleQuitAE), 0, false);
  104.                                 
  105.     if (!myErr)
  106.         {
  107.         Handle menuBar = GetNewMBar(rMenuBar);
  108.         if (menuBar != nil)
  109.             {
  110.             SetMenuBar(menuBar);
  111.             DisposHandle(menuBar);
  112.             AddResMenu(GetMenuHandle(mApple), 'DRVR');    // add DA names to Apple menu 
  113.             DrawMenuBar();
  114.             }
  115.         else
  116.             myErr = (ResError() ? ResError() : 1);
  117.         }
  118.  
  119.     SIOUXSettings.standalone = false;
  120.     SIOUXSettings.setupmenus = false;
  121.     SIOUXSettings.initializeTB = false;
  122.     
  123.     SIOUXSettings.columns = 90;
  124.     
  125.     printf("Initializing.\n");
  126.     
  127.     SIOUXSetTitle ("\pSpeech Recognition Example");
  128.     
  129.     if ((gRecSystem = MyInitSpeechRecognition ()) == NULL)
  130.         {
  131.         myErr = true;
  132.         printf("Initialization failed.\n");
  133.         }
  134.     
  135.     if (!myErr)
  136.         if ((gLanguageModel = MyBuildLanguageModel (gRecSystem)) == NULL)
  137.             myErr = true;
  138.  
  139.     if (!myErr)
  140.         {
  141.         printf("\n");
  142.         printf("This program uses a language model with following BNF:\n");
  143.         printf("<TopLM>    = <call> <person> | schedule meeting with <person> | view today's schedule;\n");
  144.         printf("<call>    = call | phone | dial;\n");
  145.         printf("<person>= Arlo | Matt | Brent | my wife;\n");
  146.         printf("\n");
  147.         }
  148.         
  149.     if (!myErr)
  150.         myErr = SRNewRecognizer (gRecSystem, &gRecognizer, kSRDefaultSpeechSource);
  151.     
  152.     if (!myErr)
  153.         myErr = MyInstallSpeechAEHandlers ();
  154.     
  155. #if SR_CALLBACKS
  156.     if (!myErr)
  157.         myErr = MyInstallNotificationCallBack (gRecognizer);
  158. #endif
  159.     
  160.     if (!myErr)
  161.         myErr = SRSetLanguageModel (gRecognizer, gLanguageModel);
  162.     
  163.     if (!myErr)
  164.         myErr = SRStartListening (gRecognizer);
  165.     
  166.     if (!myErr)
  167.         successfulStartup = true;
  168.     
  169.     if (!successfulStartup)
  170.         printf("An error occured, quitting...\n");
  171.         
  172.     return successfulStartup;
  173. }
  174.  
  175. void Terminate (void)
  176. {
  177.     printf("Terminating.\n");
  178.     
  179.     if (gListening) SRStopListening (gRecognizer);
  180.     
  181.     if (gRecognizer)
  182.         {
  183. #if SR_CALLBACKS
  184.         MyRemoveNotificationCallBack (gRecognizer);
  185. #endif
  186.         SRReleaseObject (gRecognizer);
  187.         }
  188.     
  189.     if (gLanguageModel) SRReleaseObject (gLanguageModel);
  190.     if (gRecSystem) SRCloseRecognitionSystem (gRecSystem);
  191. }
  192.  
  193. void EventLoop (void)
  194. {
  195.     while (!gFinished)
  196.         {
  197.         EventRecord        event;
  198.         unsigned char    keyChar;
  199.         
  200.         if (WaitNextEvent(everyEvent, &event, 1, nil))
  201.             {
  202.             // only handle event if SIOUX doesn't
  203.             if (!SIOUXHandleOneEvent(&event))
  204.                 switch (event.what)
  205.                     {
  206.                     case mouseDown :
  207.                         HandleMouseDown (&event);
  208.                         break;
  209.                     case keyDown :
  210.                         keyChar = event.message & charCodeMask;
  211.                         if (event.modifiers & cmdKey)
  212.                             HandleMenuSelect (MenuKey(keyChar));
  213.                         break;
  214.                     case kHighLevelEvent :
  215.                         AEProcessAppleEvent(&event);
  216.                         break;
  217.                     default :
  218.                         break;
  219.                     }
  220.             }
  221. #if SR_CALLBACKS
  222.         MyIdleCheckForSpeechResult ();
  223. #endif
  224.         }
  225. }
  226.  
  227. void HandleMouseDown (EventRecord * event)
  228. {
  229.     WindowPtr    window;
  230.     short        partCode = FindWindow (event->where, &window);
  231.     
  232.     switch (partCode)
  233.         {
  234.         case inSysWindow :
  235.             SystemClick (event, window);
  236.             break;
  237.         case inContent:
  238.             break;
  239.         case inMenuBar :
  240.             HandleMenuSelect (MenuSelect(event->where));
  241.             break;
  242.         case inDrag :
  243.             DragWindow (window, event->where, &qd.screenBits.bounds);
  244.             break;
  245.         case inGoAway:
  246.             if (TrackGoAway (window, event->where))
  247.                 gFinished = true;
  248.             break;
  249.         default :
  250.             break;
  251.         }
  252.     
  253.     HiliteMenu (0);
  254. }
  255.  
  256. void HandleMenuSelect (long menuChoice)
  257. {
  258.     short     itemNum;
  259.     Str255    daName;
  260.     GrafPtr    ourPort;
  261.     
  262.     itemNum = LoWord(menuChoice);
  263.     switch (HiWord(menuChoice))
  264.         {
  265.         case mApple :
  266.             GetItem (GetMenuHandle(mApple), itemNum, daName);
  267.             GetPort (&ourPort);
  268.             OpenDeskAcc (daName);
  269.             SetPort (ourPort);
  270.             break;
  271.         case mFile :
  272.             // all we've got is quit
  273.             gFinished = true;
  274.             break;                
  275.         default :
  276.             break;
  277.         }
  278. }
  279.  
  280. pascal OSErr DummyAEHandler (AppleEvent */*theAEevt*/, AppleEvent* /*reply*/, long /*refcon*/)
  281. {
  282.     return noErr;
  283. }
  284.  
  285. pascal OSErr HandleQuitAE (AppleEvent */*theAEevt*/, AppleEvent* /*reply*/, long /*refcon*/)
  286. {
  287.     gFinished = true;
  288.     return noErr;
  289. }
  290.